package com.mtung.cosumer.controller;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.mtung.cosumer.service.DiscoveryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author zhenguo.yao
 * @date 2020/12/11
 */
@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    private DiscoveryService discoveryService;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping
    public String consumer(@RequestParam String serviceName) throws NacosException {
        Instance instance = discoveryService.getInstance(serviceName);
        String url = "http://" + instance.getIp() + ":" + instance.getPort() + "/producer";
        String resp = restTemplate.getForObject(url, String.class);
        return "拿到响应: " + resp;
    }


}
